package com.ksd.pug.controller.article;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.pug.commons.anno.PugPermissionIngore;
import com.ksd.pug.commons.ex.PugValidationException;
import com.ksd.pug.commons.threadlocal.UserThrealLocal;
import com.ksd.pug.commons.utils.fn.asserts.Vsserts;
import com.ksd.pug.config.anno.LoginCheck;
import com.ksd.pug.config.cache.EHCacheService;
import com.ksd.pug.controller.APIBaseController;
import com.ksd.pug.pojo.ArticleComment;
import com.ksd.pug.pojo.SysLoginUser;
import com.ksd.pug.service.article.IArticleCommentService;
import com.ksd.pug.vo.comment.ArticleCommentPageVo;
import com.ksd.pug.vo.comment.ArticleCommentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hcl
 * @date 2022/3/19$ 16:59$
 */
@RestController
@Slf4j
public class ArticleCommentController extends APIBaseController {


    @Autowired
    private IArticleCommentService articleCommentService;

    @Autowired
    private EHCacheService ehCacheService;


    /**
     * 保存文章评论和回复
     *
     * @param articleCommentVo
     * @return
     */
    @PostMapping("/article/comment/save")
    public ArticleComment saveArticleComment(@RequestBody ArticleCommentVo articleCommentVo) {
        return articleCommentService.saveArticleComment(articleCommentVo);
    }

    /**
     * 评论的删除
     *
     * @param
     * articleId 文章id
     * commentId 评论id
     * @return
     */
    @PostMapping("/article/comment/del/{articleid}/{id}")
    public boolean delArticleComment(@PathVariable("articleid") Long articleId,@PathVariable("id") Long commentId) {
        SysLoginUser user = UserThrealLocal.get();
        return articleCommentService.delArticleComment(articleId,commentId, user.getId());
    }


    /**
     * 评论的查询分页
     *
     * @param articleCommentPageVo
     * @return
     */
    @LoginCheck
    @PugPermissionIngore
    @PostMapping("/article/comment/load")
    public Page<ArticleComment> loadArticleComment(@RequestBody ArticleCommentPageVo articleCommentPageVo) {
        return articleCommentService.loadArticleComment(articleCommentPageVo);
    }

    /**
     * 子集评论的查询分页
     *
     * @param articleCommentPageVo 注意：parentId必须填写
     * @return
     */
    @LoginCheck
    @PugPermissionIngore
    @PostMapping("/article/comment/loadmore")
    public Page<ArticleComment> loadArticleCommentChild(@RequestBody ArticleCommentPageVo articleCommentPageVo) {
        if (Vsserts.isNotNull(articleCommentPageVo.getParentId()) &&
                articleCommentPageVo.getParentId().equals(0L)) {
            throw new PugValidationException(602, "子集不能是0");
        }
        return articleCommentService.loadCommentChildren(articleCommentPageVo.getArticleId(), articleCommentPageVo.getParentId(),
                articleCommentPageVo.getPageNo(), articleCommentPageVo.getPageSize());
    }

    /**
     * 用户点赞
     * @return
     */
    @PostMapping("/article/comment/like/{commentId}")
    public int usercommentlike(@PathVariable("commentId")Long commentId) {
        return articleCommentService.updateCommentPlusZannum(commentId);
    }

    /**
     * 用户取消点赞
     * @return
     */
    @PostMapping("/article/comment/unlike/{commentId}")
    public int usercommentunlike(@PathVariable("commentId")Long commentId) {
        return articleCommentService.updateCommentMiusZannum(commentId);
    }

}
